package com.ly.hotel.spainterface.pp.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.pp.adapter.PpXFActionAdapter;
import com.ly.hotel.spainterface.pp.bean.PpXFPluginBean;
import com.ly.hotel.spainterface.pp.bean.PreXFDataBean;
import com.ly.hotel.spainterface.pp.event.PpDialogCloseEvent;
import com.ly.hotel.util.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by mo on 2018/6/7.
 */

public class PpXFDetailsDialog extends Dialog {

    private final String TAG = "PpXFDetailsDialog";
    private Context context;
    private int rod = 0;
    private int consumeItemNum = 1;// 商品项目默认的数目
    private PreXFDataBean preXFDataBean;
    private PpXFPluginBean ppXFPluginBean;
    private MachineBean machineBean;
    private boolean isOpenFromQuickMenu;
    private Button consumeDetailsClose;
    private TextView consumeItemPrice;
    private TextView consumeItemName;
    private ImageView consumeDetialImage;
    private LinearLayout consumeActionTips;
    private GridView consumeActionGridview;
    private ImageButton consumeBtnSub;
    private TextView consumeItemCount;
    private ImageButton consumeBtnAdd;
    private TextView cannotOrderTips;
    private Button callWaiter;
    private Button consumeBuyNow;
    private String actionName;
    private final MyApplication app;

    public PpXFDetailsDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        rod = context.getResources().getDimensionPixelSize(R.dimen.px6);
        app = (MyApplication) MyApplication.getContext();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(PpDialogCloseEvent ppDialogCloseEvent) {
        dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pp_xf_details_layout);
        initView();
        setXFDetailsClose();
        setXFDetailsData();
        setXFAction();
        setConsumeSubAndAdd();
        setCallWaiter();
        setOrderConsume();
    }

    private void initView() {
        consumeDetailsClose = findViewById(R.id.consume_details_close);
        consumeItemPrice = findViewById(R.id.consume_item_price);
        consumeItemName = findViewById(R.id.consume_item_name);
        consumeDetialImage = findViewById(R.id.consume_detial_image);
        consumeActionTips = findViewById(R.id.consume_action_tips);
        consumeActionGridview = findViewById(R.id.consume_action_gridview);
        consumeBtnSub = findViewById(R.id.consume_btn_sub);
        consumeItemCount = findViewById(R.id.consume_item_count);
        consumeBtnAdd = findViewById(R.id.consume_btn_add);
        cannotOrderTips = findViewById(R.id.cannot_order_tips);
        callWaiter = findViewById(R.id.call_waiter);
        consumeBuyNow = findViewById(R.id.consume_buy_now);
    }

    private void setXFDetailsClose() {
        consumeDetailsClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void setXFDetailsData() {
        String imageUrl = MyApplication.getHostResUrl() + "/upload/" + preXFDataBean.getImg();
        ImageUtil.displayImage_Round(context, imageUrl, consumeDetialImage, rod);
        consumeItemPrice.setText(preXFDataBean.getPrice() + "元/" + preXFDataBean.getMeasure());
        consumeItemName.setText(preXFDataBean.getName());
    }

    // 设置消费做法
    private void setXFAction() {
        if (TextUtils.isEmpty(preXFDataBean.getNotes())) {
            consumeActionTips.setVisibility(View.VISIBLE);
        } else {
            try {
                String[] actionArray = preXFDataBean.getNotes().replace(" ", "").split(",");
                final List<String> actions = new ArrayList<>();
                for (int i = 0; i < actionArray.length; i++) {
                    actions.add(actionArray[i]);
                }
                if (actions.size() > 0) {
                    final PpXFActionAdapter ppXFActionAdapter = new PpXFActionAdapter(context, actions);
                    consumeActionGridview.setAdapter(ppXFActionAdapter);
                    consumeActionGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            ppXFActionAdapter.setDefaultPosition(position);
                            // 商品的做法
                            actionName = actions.get(position);
                            Log.i(TAG, "商品做法：" + actionName);
                        }
                    });
                    consumeActionGridview.performItemClick(consumeActionGridview.getChildAt(0), 0,
                            consumeActionGridview.getItemIdAtPosition(0));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 消费项目数目的加减
    private void setConsumeSubAndAdd() {
        consumeBtnSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (consumeItemNum > 1) {
                    consumeItemNum--;
                    consumeItemCount.setText(consumeItemNum + "");
                } else {
                    CommentCustomToast.makeText(context, "购买的商品数量不能低于1", Toast.LENGTH_SHORT, false).show();
                }
            }
        });

        consumeBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ppXFPluginBean.getGoodsCountLimit() > 0) {
                    if (consumeItemNum < ppXFPluginBean.getGoodsCountLimit()) {
                        consumeItemNum++;
                        consumeItemCount.setText(consumeItemNum + "");
                    } else {
                        CommentCustomToast.makeText(context, "购买的商品数量不能高于" + ppXFPluginBean.getGoodsCountLimit(),
                                Toast.LENGTH_SHORT, false).show();
                    }
                } else {
                    // 不填goodsCountLimit则默认，不能高于3份；
                    if (consumeItemNum < 3) {
                        consumeItemNum++;
                        consumeItemCount.setText(consumeItemNum + "");
                    } else {
                        CommentCustomToast.makeText(context, "购买的商品数量不能高于" + 3,
                                Toast.LENGTH_SHORT, false).show();
                    }
                }
            }
        });
    }

    // 呼叫服务员
    private void setCallWaiter() {
        if (ppXFPluginBean.isShowCall()) {
            if (app.isBtnItemEnable("呼叫")) {// 判断是否开通呼叫功能
                String callText = "";
                callWaiter.setVisibility(View.VISIBLE);
                if (app.getCallStatus() == Constants.CALL_CANCLE) {
                    callText = "立即呼叫";
                } else if (app.getCallStatus() == Constants.CALL_REQUEST) {
                    callText = "取消呼叫";
                }
                callWaiter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                app.changeLight();
                            }

                        }).start();

                        if (app.getCallStatus() == Constants.CALL_CANCLE) {
                            EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_POINT_CALL));
                        } else {
                            EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_POINT_CANCLE_CALL));
                        }
                    }
                });
                callWaiter.setText(callText);
            } else {
                cannotOrderTips.setVisibility(View.VISIBLE);
                callWaiter.setVisibility(View.GONE);
            }
        } else {
            cannotOrderTips.setVisibility(View.VISIBLE);
            callWaiter.setVisibility(View.GONE);
        }
    }

    public void onEventMainThread(SendLogEvent sendLogEvent) {
        if (app.getCallStatus() == Constants.CALL_CANCLE) {
            callWaiter.setText("取消呼叫");
        } else {
            callWaiter.setText("立即呼叫");
        }
    }

    private void setOrderConsume() {
        if (!TextUtils.isEmpty(ppXFPluginBean.getMode())) {
            if (ppXFPluginBean.getMode().equals("1")) {
                consumeBuyNow.setVisibility(View.VISIBLE);
                cannotOrderTips.setVisibility(View.GONE);
                callWaiter.setVisibility(View.GONE);
            } else {
                consumeBuyNow.setVisibility(View.GONE);
            }
        }

        consumeBuyNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PpXFOrderDialog ppXFOrderDialog = new PpXFOrderDialog(context, R.style.introDialog);
                ppXFOrderDialog.setPpXFPluginBean(ppXFPluginBean);
                ppXFOrderDialog.setPreXFDataBean(preXFDataBean);
                ppXFOrderDialog.setConsumeItemNum(consumeItemNum);
                ppXFOrderDialog.setActionName(actionName);
                ppXFOrderDialog.setMachineBean(machineBean);
                ppXFOrderDialog.setCanceledOnTouchOutside(false);
                if (isOpenFromQuickMenu) {
                    ppXFOrderDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
                }
                ppXFOrderDialog.show();
            }
        });
    }

    public void setPreXFDataBean(PreXFDataBean preXFDataBean) {
        this.preXFDataBean = preXFDataBean;
    }

    public void setPpXFPluginBean(PpXFPluginBean ppXFPluginBean) {
        this.ppXFPluginBean = ppXFPluginBean;
    }

    public void setMachineBean(MachineBean machineBean) {
        this.machineBean = machineBean;
    }

    public void setOpenFromQuickMenu(boolean openFromQuickMenu) {
        isOpenFromQuickMenu = openFromQuickMenu;
    }
}
